package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.quality.constant.Constants;
import com.ruibang.glass.quality.domain.req.QualityDeflectReq;
import com.ruibang.glass.quality.domain.req.SemiCheckReportReq;
import com.ruibang.glass.quality.domain.resp.SemiCheckDetailData;
import com.ruibang.glass.quality.entity.QualityDeflect;
import com.ruibang.glass.quality.entity.SpecsConfig;
import com.ruibang.glass.quality.mapper.QualityDeflectMapper;
import com.ruibang.glass.quality.service.QualityDeflectService;
import com.ruibang.glass.quality.service.SpecsConfigService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.constant.CommonConstant;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 半成品挠度信息 服务实现类
 * </p>
 *
 * @author wujie
 * @since 2024-03-11
 */
@Service
public class QualityDeflectServiceImpl extends ServiceImpl<QualityDeflectMapper, QualityDeflect> implements QualityDeflectService {

//    @Autowired
//    private RedisSingleService redisService;

    @Autowired
    private RedisSingleService redisService;

    @Autowired
    private SpecsConfigService specsConfigService;

    @Override
    public void saveInfo(QualityDeflect qualityDeflect) {
        LocalDateTime now = LocalDateTime.now();
        if(qualityDeflect.getTransectTime()==null){
            qualityDeflect.setTransectTime(now);
        }
        if(qualityDeflect.getRecordTime()==null){
            qualityDeflect.setRecordTime(now);
        }

        //更新文件对应业务Id
        this.save(qualityDeflect);
    }

    @Override
    public void updateInfo(QualityDeflect qualityDeflect) {
        //更新文件信息
        this.updateById(qualityDeflect);
    }

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(QualityDeflect::getDeflectId, id).set(QualityDeflect::getIsDelete, Constants.ONE_STR).update();
    }


    @Override
    public PageResult<QualityDeflect> queryPage(PageRequest<QualityDeflectReq> pageRequest) {
        final QualityDeflectReq param = pageRequest.getParam();
        final LambdaQueryWrapper<QualityDeflect> lambda = new QueryWrapper<QualityDeflect>().lambda();
        this.buildCondition(lambda,param);
        lambda.orderBy(true,false, QualityDeflect::getTransectTime);
        final IPage<QualityDeflect> page = this.page(new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize()), lambda);
        List<SpecsConfig> configs = specsConfigService.getSpecsConfigByIds(page.getRecords().stream().map(qt->qt.getCheckRule()).collect(Collectors.toList()));
        Map<String, SpecsConfig> configMap = null;
        if(CollectionUtils.isNotEmpty(configs)){
            configMap = configs.stream().collect(Collectors.toMap(c->c.getSpecsId(),c->c,(c1,c2)->c1));
        }

        Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);

        for (QualityDeflect record : page.getRecords()) {
            if(userMap!=null){
                record.setUserName(userMap.get(record.getUserId())==null?"":userMap.get(record.getUserId()).toString());
            }
            if(configMap!=null){
                record.setSpecsConfig(configMap.get(record.getCheckRule()));
            }
        }
        PageResult<QualityDeflect> pr = new PageResult(page);
        return pr;
    }

    @Override
    public List<QualityDeflect> queryQualityDeflect(QualityDeflectReq contactReq) {
        final LambdaQueryWrapper<QualityDeflect> lambda = new QueryWrapper<QualityDeflect>().lambda();
        this.buildCondition(lambda,contactReq);
        lambda.orderBy(true,true, QualityDeflect::getTransectTime);
        List<QualityDeflect> list = this.list(lambda);
        if(CollectionUtils.isNotEmpty(list)){
            Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);
            Map<Object, Object> productMap = redisService.hmget(RedisKey.PRODUCT_LINE_GROUP_TYPE);
            for (QualityDeflect qd : list) {
                if(userMap!=null){
                    qd.setUserName(userMap.get(qd.getUserId())==null?"":userMap.get(qd.getUserId()).toString());
                }
                if(productMap!=null){
                    qd.setProductLineName(productMap.get(qd.getProductLine())==null?"":productMap.get(qd.getProductLine()).toString());
                }
            }
        }
        return list;
    }

    @Override
    public List<SemiCheckDetailData> queryDeflectCheckData(SemiCheckReportReq reportReq) {
        return baseMapper.queryDeflectCheckData(reportReq);
    }

    /**
     * 构造查询条件
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<QualityDeflect> lambda, QualityDeflectReq param){
        lambda.eq(QualityDeflect::getIsDelete, CommonConstant.ZERO_STR);
        if(null == param) return;
        if(StringUtils.isNotEmpty(param.getDeflectId())){
            lambda.eq(QualityDeflect::getDeflectId, param.getDeflectId());
        }
        if(StringUtils.isNotEmpty(param.getProductLine())){
            lambda.eq(QualityDeflect::getProductLine, param.getProductLine());
        }
        if(StringUtils.isNotEmpty(param.getGlassId())){
            lambda.eq(QualityDeflect::getGlassId, param.getGlassId());
        }
        if(StringUtils.isNotEmpty(param.getGlassType())){
            lambda.eq(QualityDeflect::getGlassType, param.getGlassType());
        }
        if(StringUtils.isNotEmpty(param.getGlassModel())){
            lambda.eq(QualityDeflect::getGlassModel, param.getGlassModel());
        }
        if(StringUtils.isNotEmpty(param.getCheckRule())){
            lambda.eq(QualityDeflect::getCheckRule, param.getCheckRule());
        }
        if(StringUtils.isNotEmpty(param.getUserId())){
            lambda.eq(QualityDeflect::getUserId, param.getUserId());
        }
        if(param.getRecordStartTime()!=null&&param.getRecordEndTime()!=null){
            lambda.ge(QualityDeflect::getRecordTime, param.getRecordStartTime());
            lambda.le(QualityDeflect::getRecordTime, param.getRecordEndTime());
        }
        if(param.getTransectStartTime()!=null&&param.getTransectEndTime()!=null){
            lambda.ge(QualityDeflect::getTransectTime, param.getTransectStartTime());
            lambda.le(QualityDeflect::getTransectTime, param.getTransectEndTime());
        }
        
        if(StringUtils.isNotEmpty(param.getTeam())){
            lambda.eq(QualityDeflect::getTeam, param.getTeam());
        }
        if(StringUtils.isNotEmpty(param.getDecide())){
            lambda.eq(QualityDeflect::getDecide, param.getDecide());
        }
    }
}
